package com.luyuan.crm.sale.web.provider;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.springframework.stereotype.Component;

import com.luyuan.crm.formula.model.Formula;
import com.luyuan.crm.formula.service.FormulaService;
import com.luyuan.crm.product.model.ProductType;
import com.luyuan.crm.sale.web.OrderWizardAction;
import com.ruijie.blueprint.spring.context.ApplicationContextHolder;
import com.ruijie.web.jsf.provider.SelectItemsProvider;

@Component("formulaProvider")
public class FormulaProvider implements SelectItemsProvider {
	@Resource(name = "formulaService")
	private FormulaService formulaService;

	@Override
	public List<SelectItem> getSelectItems(Object... parameters) {
		OrderWizardAction orderWizardAction = (OrderWizardAction) ApplicationContextHolder
				.getApplicationContext().getBean("orderWizardAction");
		ProductType productType = orderWizardAction.getProductType();
		List<SelectItem> items = new ArrayList<SelectItem>();
		List<Formula> formulas = formulaService.findByProductType(productType);
		for (Formula formula : formulas) {
			items.add(new SelectItem(formula, formula.getName()));
		}
		return items;
	}

	// @Override
	// public List<Formula> getSuggestionItems(Object... arg0) {
	// OrderWizardAction orderWizardAction = (OrderWizardAction)
	// ApplicationContextHolder
	// .getApplicationContext().getBean("orderWizardAction");
	// ProductTypeModel productType = orderWizardAction.getProductType();
	// return formulaService.findByProductType(productType);
	// }

	public Object getRequestAttribute(String key) {
		return FacesContext.getCurrentInstance().getExternalContext()
				.getRequestMap().get(key);
	}
}
